package com.linxz.ui.widgets.images;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.linxz.ui.pojo.PreImageBean;
import com.linxz.ui.R;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.luck.picture.lib.tools.ScreenUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @author Linxz
 * 创建日期：2019年10月19日 23:10
 * version：v4.5.4
 * 描述：图片上传的时候展示的图
 */
public class ImageUploadLayout extends FrameLayout {

    private List<ImageUploadBean> imagePaths = new ArrayList<>();
    private ImageUpLoadAdapter adapter;
    private ImageUploadLayoutListener layoutListener;
    private int maxCount;

    public ImageUploadLayout(@NonNull Context context) {
        super(context);
    }

    public ImageUploadLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImageUploadLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        setListener();
        initData();
    }

    private void initView() {
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.ui_layout_image_upload, this);
        RecyclerView rvImages = contentView.findViewById(R.id.rvImages);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        rvImages.addItemDecoration(new GridSpacingItemDecoration(4, ScreenUtils.dip2px(getContext(), 5), false));
        rvImages.setLayoutManager(gridLayoutManager);
        adapter = new ImageUpLoadAdapter(imagePaths);
        rvImages.setAdapter(adapter);
    }

    private void setListener() {
        adapter.setOnImageItemClickListener(new ImageUpLoadAdapter.OnImageItemClickListener() {
            @Override
            public void onPicker(ImageUploadBean imageUploadBean, int position) {
                if (layoutListener != null) {
                    List<PreImageBean> paths = new ArrayList<>();
                    for (ImageUploadBean uploadBean : imagePaths) {
                        if (!uploadBean.isPickerBean()) {
                            PreImageBean bean = new PreImageBean(uploadBean.getPath());
                            paths.add(bean);
                        }
                    }
                    layoutListener.onPreView(paths, position);
                }
            }

            @Override
            public void onDelete(ImageUploadBean imageUploadBean, int position) {
                deleteImage(imageUploadBean, position);
            }

            @Override
            public void onAdd() {
                if (layoutListener != null) {
                    layoutListener.onAdd();
                }
            }
        });
    }

    private void initData() {
        imagePaths.add(new ImageUploadBean("", false, true));
        adapter.notifyDataSetChanged();
    }

    public void setLayoutListener(ImageUploadLayoutListener layoutListener) {
        this.layoutListener = layoutListener;
    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    public void setImages(List<String> images) {
        for (String path : images) {
            int realImageSize = imagePaths.size() - 1;
            if (realImageSize < maxCount) {
                imagePaths.add(realImageSize, new ImageUploadBean(path, false, false));
            }
        }
        int realImageSize = imagePaths.size() - 1;
        if (realImageSize == maxCount) {
            for (int i = 0; i < imagePaths.size(); i++) {
                if (imagePaths.get(i).isPickerBean()) {
                    imagePaths.remove(i);
                    break;
                }
            }
        }
        adapter.notifyDataSetChanged();
    }

    private void deleteImage(ImageUploadBean imageUploadBean, int position) {
        adapter.remove(position);
        adapter.notifyDataSetChanged();
        boolean hasAddButton = false;
        for (ImageUploadBean bean : imagePaths) {
            if (bean.isPickerBean()) {
                hasAddButton = true;
                break;
            }
        }
        if (!hasAddButton) {
            imagePaths.add(new ImageUploadBean("", false, true));
            adapter.notifyDataSetChanged();
        }
    }

    public List<String> getImagePaths() {
        List<String> paths = new ArrayList<>();
        for (ImageUploadBean bean : imagePaths) {
            if (!bean.isPickerBean())
                paths.add(bean.getPath());
        }
        return paths;
    }

    public interface ImageUploadLayoutListener {
        void onPreView(List<PreImageBean> paths, int position);

        void onAdd();
    }
}
